<template>
	<view class="content">
		<view class="flex cloud-btn">
			<button @click="AddBtn">上传文章</button>
			<button @click="getData">获取列表</button>
			<button @click="updateArticle">更新列表</button>
		</view>
		<view class="btn-group mt20">
			<view class="btn"  @click="AddArticleBtn">
				添加文章
			</view>
		</view>
		<view class="list">
			<view class="item" v-for="(a,i) in listData" :key="i" @click="goDtl(i)">
				<view class="title">
					{{a.title}}
				</view>
				<view class="info">
					{{a.content}}
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: 'Hello',
				listData:[],
			}
		},
		onLoad() {

		},
		methods: {
			AddArticleBtn(){
				uni.navigateTo({
					url:'./addArticle'
				})
			},
			goDtl(i){
				uni.navigateTo({
					url:'./articleDtl?id='+ this.listData[i]._id
				})
			},
			AddBtn(){
				uniCloud.callFunction({
					name:'addArticle',
					data:{
						title:"文字和图片",
						content:"文字雷霆"
					}
				}).then(res=>{
					console.log(res.result);
				}).catch(err =>{
					console.log(err);
				})
			},
			getData(){
				uniCloud.callFunction({
					name:'queryArticles',
					data:{}
				}).then(resp=>{
					// console.log(resp.result);
					let res = resp.result;
					if(res.code==1){
						this.listData = res.data.data;
					}
				}).catch(err=>{
					console.log('err',err);
				})
			},
			deepCopy(target){
				let obj=null;
				for(let key of target){
					if(typeof target ){
						
					}
				}
			},
			updateArticle(){
				let uid = this.listData[0]._id;
				uniCloud.callFunction({
					name:'updateArticle',
					data:{
						id:uid,
						content:"文章内容更新了"
					}
				}).then(res=>{
					console.log('res',res);
				}).catch(err =>{
					console.log('err',err);
				})
			}
		},
		onShow(){
			this.getData();
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
	.list{
		width: 690rpx;
		background-color: #FFFFFF;
		border-radius: 10rpx;
		margin-top: 30rpx;
		.item{
			padding: 20rpx 20rpx;
			border-bottom:1rpx solid #C8C7CC;
			font-size: 26rpx;
			&:last-child{
				border-bottom: none;
			}
			.title {
				font-size: 28rpx;
				color: #333;
				font-weight: 550;
			}
			.info{
				padding-top:10rpx;
				line-height: 1.4;
				max-height: 2.8em;
				overflow: hidden;
				display: -webkit-box;
				-webkit-box-orient: vertical;
				-webkit-line-clamp: 2;
			}
		}
	}
	.label{
		width: 240rpx;
		color: #666;
	}
	.cloud-btn{
		button{
			background: linear-gradient(92deg,#ffba40,#ff1b40);
			color: #FFFFFF;
			&:nth-child(2){
				margin: 0 10rpx;
			}
		}
	}
	.add-btn{
		margin: 40rpx auto 0;
		width: 540rpx;
		height: 80rpx;
		text-align: center;
		line-height: 80rpx;
		border-radius: 10rpx;
		border:1rpx solid #EEEEEE;
	}
	.btn-group{
		display: flex;
		justify-content: space-between;
		.btn{
			width: 150rpx;
			height: 40rpx;
			text-align: center;
			line-height: 40rpx;
			border-radius: 20rpx;
			border: 1rpx solid $btnBg;
			color: $btnBg;
			font-size: 28rpx;
		}
	}
</style>
